package cn.kgc.shiro03.handler;

import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: 课工场
 * @Version: v1.0  2023/7/28
 * @Description:
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(IncorrectCredentialsException.class)
    public Map<String,Object> incorrectCredentialsExceptionHandler(IncorrectCredentialsException e){
        e.printStackTrace();
        HashMap<String, Object> map = new HashMap<>();
        map.put("code",500);
        map.put("msg","密码错误");
        return map;
    }

    @ExceptionHandler(AuthorizationException.class)
    public Map<String,Object> authorizationExceptionHandler(AuthorizationException e){
        e.printStackTrace();
        HashMap<String, Object> map = new HashMap<>();
        map.put("code",500);
        map.put("msg","权限不足,请联系管理员!!!");
        return map;
    }


    @ExceptionHandler(UnknownAccountException.class)
    public Map<String,Object> unknownAccountExceptionHandler(UnknownAccountException e){
        e.printStackTrace();
        HashMap<String, Object> map = new HashMap<>();
        map.put("code",500);
        map.put("msg","用户名错误");
        return map;
    }

}
